import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_config.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../main/dnp_main_controller.dart';

class DnpSignOutPage extends StatelessWidget {
  const DnpSignOutPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Get.back();
      },
      child: Scaffold(
        backgroundColor:Colors.black.withValues(alpha: 0.5),
        body: Center(
          child: GestureDetector(
            onTap: (){},
            child: Container(
              width:309,
              height: 372,
              decoration: BoxDecoration(
               image: DecorationImage(
                 image: AssetImage('assets/images/ic_sign_out_bg.webp'),
                 fit: BoxFit.fill
               ),
              ),
              child: Column(
                children: [
                  SizedBox(height: 168),
                  Text(
                    "Sign out",
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF252630)
                    ),
                  ),
                  SizedBox(height: 8),
                  Container(
                    height: 88,
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(left: 14,right: 27),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(16),
                      color: Color(0xFFF1F1F2)
                    ),
                    child: Text(
                      'You are one step away from getting a loan, are you sure you want to back out?',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                        color: Color(0xFF252630)
                      ),
                    ),
                  ),
                  SizedBox(height: 14),
                  Padding(
                    padding: const EdgeInsets.only(left: 14,right: 27),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: GestureDetector(
                            onTap: () => _signOut(context),
                            child: Container(
                              height: 48,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(16),
                                color: Color(0xFFCFE6FF)
                              ),
                              child: Text(
                                'Logout',
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w600,
                                  color: Color(0xFF95A5B9)
                                )
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: 9),
                        Expanded(
                          child: GestureDetector(
                            onTap: () => _back(context),
                            child: Container(
                              height: 48,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(16),
                                  color: Color(0xFF1D4AC8)
                              ),
                              child: Text(
                                  'Go to loan',
                                  style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w600,
                                      color: Colors.white
                                  )
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void _signOut(BuildContext context) async {

    DnpPluginManager().showLoading();

    var result = await DnpNetManager().get(
      '/haseye/sleeps'
    );

    DnpPluginManager().hideLoading();

    if (result.success) {
      DnpNetConfig().saveKnowledge("");
      DnpCacheManager().saveString("ho_phone", "");
      _back(context);
    }else {
      DnpPluginManager().showToast(result.kurios ?? "");
    }

  }

  void _back(BuildContext context) {
    Navigator.pop(context);
    Navigator.pop(context);
    Get.find<DnpMainController>().scrollToHomeWidget();
  }
}
